/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#ifndef _HDFS_LIBHDFS3_CLIENT_INPUTSTREAMIMPL_H_
#define _HDFS_LIBHDFS3_CLIENT_INPUTSTREAMIMPL_H_


#include <kmhdfs/client/block_reader.h>
#include <kmhdfs/common/exception_internal.h>
#include <kmhdfs/client/file_system.h>
#include <kmhdfs/common/hash.h>
#include <kmhdfs/client/input_stream_inter.h>
#include <memory>
#include <kmhdfs/client/peer_cache.h>
#include <kmhdfs/rpc/rpc_auth.h>
#include <kmhdfs/server/datanode.h>
#include <kmhdfs/server/located_block.h>
#include <kmhdfs/server/located_blocks.h>
#include <kmhdfs/common/session_config.h>
#include <turbo/container/flat_hash_map.h>
#include <kmhdfs/common/lock_vector.h>

#ifdef MOCK
#include <tests/mock/test_datanode_stub.h>
#endif

namespace Hdfs {
namespace Internal {

/**
 * A input stream used read data from hdfs.
 */
class InputStreamImpl: public InputStreamInter {
public:
    InputStreamImpl();
    explicit InputStreamImpl(std::shared_ptr<LocatedBlocks> lbs);
    ~InputStreamImpl();

    /**
     * Open a file to read
     * @param fs hdfs file system.
     * @param path the file to be read.
     * @param verifyChecksum verify the checksum.
     */
    void open(std::shared_ptr<FileSystemInter> fs, const char * path, bool verifyChecksum);

    /**
     * To read data from hdfs.
     * @param buf the buffer used to filled.
     * @param size buffer size.
     * @return return the number of bytes filled in the buffer, it may less than size.
     */
    int32_t read(char * buf, int32_t size);

    /**
     * To read data from hdfs.
     * @param buf the buffer used to filled.
     * @param size buffer size.
     * @param position the position to seek.
     * @return return the number of bytes filled in the buffer, it may less than size.
     */
    int32_t pread(char * buf, int32_t size, int64_t position);

    /**
     * To read data from hdfs, block until get the given size of bytes.
     * @param buf the buffer used to filled.
     * @param size the number of bytes to be read.
     */
    void readFully(char * buf, int64_t size);

    int64_t available();

    /**
     * To move the file point to the given position.
     * @param pos the given position.
     */
    void seek(int64_t pos);

    /**
     * To get the current file point position.
     * @return the position of current file point.
     */
    int64_t tell();

    /**
     * Close the stream.
     */
    void close();

    /**
     * Convert to a printable string
     *
     * @return return a printable string
     */
    std::string toString();

protected:
    bool choseBestNode();
    bool isLocalNode();
    bool isLocalNode(DatanodeInfo & curNode);
    int32_t readInternal(char * buf, int32_t size);
    int32_t preadInternal(char * buf, int32_t size, int64_t position);
    std::vector<std::shared_ptr<LocatedBlock>> getBlockRange(int64_t offset, int64_t length);
    std::vector<std::shared_ptr<LocatedBlock>> getFinalizedBlockRange(int64_t offset, int64_t length);
    std::shared_ptr<LocatedBlock> fetchBlockAt(int64_t offset, int64_t length, bool useCache);
    virtual void fetchBlockByteRange(std::shared_ptr<LocatedBlock> curBlock, int64_t start, int64_t end, char * buf);
    void setupBlockReader(bool temporaryDisableLocalRead, std::shared_ptr<BlockReader> & blockReader,
                          std::shared_ptr<LocatedBlock> curBlock, int64_t start, int64_t end,
                          DatanodeInfo & curNode);
    bool choseBestNode(std::shared_ptr<LocatedBlock> curBlock, DatanodeInfo & curNode);
    const LocatedBlock * findBlockWithLock();
    int32_t readOneBlock(char * buf, int32_t size, bool shouldUpdateMetadataOnFailure);
    int64_t getFileLength();
    int64_t readBlockLength(const LocatedBlock & b);
    void checkStatus();
    void openInternal(std::shared_ptr<FileSystemInter> fs, const char * path,
                      bool verifyChecksum);
    void readFullyInternal(char * buf, int64_t size);
    void seekInternal(int64_t pos);
    void seekToBlock(const LocatedBlock & lb);
    void setupBlockReader(bool temporaryDisableLocalRead);
    void updateBlockInfos();
    void updateBlockInfos(bool need);

    /**
     * Get block at the specified position.
     * Fetch it from the namenode if not cached.
     *
     * @param offset block corresponding to this offset in file is returned
     * @return located block
     */
    std::shared_ptr<LocatedBlock> getBlockAt(int64_t offset);

protected:
    bool closed;
    bool localRead;
    bool readFromUnderConstructedBlock;
    bool verify;
    DatanodeInfo curNode;
    exception_ptr lastError;
    FileStatus fileInfo;
    int maxGetBlockInfoRetry;
    int64_t cursor;
    int64_t endOfCurBlock;
    int64_t lastBlockBeingWrittenLength;
    int64_t prefetchSize;
    PeerCache *peerCache;
    RpcAuth auth;
    std::shared_ptr<BlockReader> blockReader;
    std::shared_ptr<FileSystemInter> filesystem;
    std::shared_ptr<LocatedBlock> curBlock;
    // state shared by stateful and positional read:
    // (protected by mutex on infoMutex)
    std::shared_ptr<LocatedBlocks> lbs;
    std::shared_ptr<SessionConfig> conf;
    std::string path;
    LockVector<DatanodeInfo> failedNodes;
    std::vector<char> localReaderBuffer;
    // mutex for state shared between read and pread
    std::recursive_mutex infoMutex;

#ifdef MOCK
private:
    Hdfs::Mock::TestDatanodeStub * stub;
#endif
};

std::unordered_set<std::string> BuildLocalAddrSet();

}
}

#endif /* _HDFS_LIBHDFS3_CLIENT_INPUTSTREAMIMPL_H_ */
